package com.ihrm.company.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.DepartmentService;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.vo.OrgThreeInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/company")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * @deprecated 添加部门
     * @param department
     * @return
     */
    @PostMapping("/departments")
    public Result add(@RequestBody Department department){
        departmentService.add(department);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 修改部门信息
     * @param id
     * @param department
     * @return
     */
    @PutMapping("/departments/{id}")
    public Result update(@PathVariable(name = "id") String id, @RequestBody Department department){
        departmentService.update(department,id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/departments/{id}")
    public Result delete(@PathVariable(name = "id") String id){
        departmentService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据 name 查询
     * @param name
     * @return
     */
    @GetMapping("/departments/{name}")
    public Result findById(@PathVariable(name = "name") String name){
        List<Department> departments =  departmentService.findByName(name);
        Result result = new Result(ResultCode.SUCCESS,departments);
        return result;
    }

    /**
     * 组织架构列表
     * @return
     */
    @GetMapping("/departments")
    public Result findAll(){
        List<OrgThreeInfoVo> list = departmentService.findAll();
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(list);
        return result;
    }

}
